<?php

// +----------------------------------------------------------------------
// | 热门活动信息
// +----------------------------------------------------------------------
// | Author: Helly
// +----------------------------------------------------------------------
// | Date: 2015/01/05 19:12
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;

class ActivityController extends AdminBase {

    //排序更新
    public function sort(){
        foreach ($_POST['sort'] as $id => $sort) {
            M('Activity')->where(array('id' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
        exit;
    }


    //新闻列表
    public function index() {
        if (IS_POST) {
            $this->redirect('index', $_POST);
        }
        //搜索
        $search = I('get.search');
        if (!empty($search)) {
            $this->assign("search", $search);
            

            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("keyword", $keyword);
                $where['title'] = array("LIKE", "%{$keyword}%");
            }
        }else{
            $this->assign("status",2);
        }
        $count = M('Activity')->where($where)->count();
        $page = $this->page($count, 20);
        $list = M('Activity')->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();
        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        session('backurl',get_url()); //页面跳转SESSION
        $this->display();
    }


    //添加信息
    public function add() {
        if (IS_POST) {
            $post=$_POST;
            if(empty($post['title'])){
                $this->error('信息标题');
            }
            $post['create_time']=time();
            if (D('Admins/Activity')->addModel($post)) {
                    $this->success("信息添加成功！", U('Activity/index'));
            } else {
                $error = D('Admins/Activity')->getError();
                $this->error($error ? $error : '信息添加失败！');            }
        } else {
            $this->display();
        }
    }

    //编辑信息
    public function edit(){
        if(IS_POST){
            if (false !== D('Admins/Activity')->editModel($_POST)) {
                $backurl=session('backurl');
                session('backurl',null);
                $this->success("信息更新成功！",$backurl);
            } else {
                $error = D('Admins/Activity')->getError();
                $this->error($error ? $error : '信息修改失败！');
            }
        }else{
            $id=I('get.id',0,'intval');
            $where=array('id'=>$id);
            $this->info=M('Activity')->where($where)->find();
            $this->display();
        }
    }

    //单个删除
    public function delete() {
        $id = I('get.id');
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        //执行删除
        if (D('Admins/Activity')->deleteBatch($id)) {
            $this->success("删除成功！");
        } else {
            $this->error(D('Admins/Activity')->getError()? : '删除失败！');
        }
    }


    //批量处理信息(审核,取消审核,删除)
    public function batch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
                case 'checktj':
                    $data=array('iftj'=>true);
                    $msg='推荐';
                    break;
                case 'canceltj':
                    $data=array('iftj'=>false);
                    $msg='取消推荐';
                    break;
            }
            foreach ($ids as $id) {
                M('Activity')->where(array('id'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }


    //批量删除信息及信息附件
    public function deleteAll(){
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            foreach ($ids as $id) {
                if(D('Admins/Activity')->deleteBatch($id)){
                    continue;
                }else{
                    $error = D('Admins/Activity')->getError();
                    $this->error($error ? $error : '删除信息失败！');
                }
            }
            $this->success('信息删除成功！');
        }
    }


}
